package com.yc.demo.d0110;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/login.s")
public class loginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String name = req.getParameter("name");
        final String pwd = req.getParameter("pwd");
        if ("yc".equals(name) && "123".equals(pwd)){
            //会话对象，相应重定向可以用会话属性传递数据
            final HttpSession session = req.getSession();
            session.setAttribute("loginedUser", name);
            resp.sendRedirect("home.jsp");
        }else {
            //Attribute请求属性，请求转发可以使用属性传递数据
            req.setAttribute("msg", "用户名或密码错误");
            req.getRequestDispatcher("Login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
